package titan.utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Class that has static methods for displaying content window,
 * file dialog box and folder dialog box;
 */
public class SWTUtils {

	/**
	 * Opens a window with text content
	 * @param shell - the parent shell
	 * @param title - the window title
	 * @param content - the content to be displayed
	 */
	public static void openContentDialog(Shell shell, String title, String content){
		// create window with the given title
		Shell dialog = new Shell(shell);
		dialog.setText(title);
		// set layout to FillLayout
		dialog.setLayout(new FillLayout());
		// create a scrollable and not editable multi text field
		Text text = new Text(dialog, SWT.MULTI|SWT.V_SCROLL|SWT.WRAP);
		text.setText(content);
		text.setEditable(false);
		// get parent size
		Rectangle r = shell.getBounds();
		// set limit text size
		text.setSize(r.width-50, r.height-50);
		// find best window size according to the text size
		int height = (int)((text.getLineCount())*text.getLineHeight())+40; //fitHeight
		if(height < 100)
			height = 100; //minHeight
		else if(height > r.height-50)
			height = r.height-50; //maxHeight
		// set window size
		dialog.setBounds(r.x+25, r.y+25, r.width-50, height);
		// open window
		dialog.open();
	}

	/**
	 * @see DirectoryDialog#open()
	 */
	public static String openFolderDialog(Shell shell) {
		return new DirectoryDialog(shell).open();
	}

	/**
	 * @see FileDialog#open()
	 * @see FileDialog#setFilterNames(String[])
	 * @see FileDialog#setFilterExtensions(String[])
	 */
	public static String openFileDialog(Shell shell, String[] filterNames, String[] filterExts) {
		FileDialog dlg = new FileDialog(shell);
		dlg.setFilterNames(filterNames);
		dlg.setFilterExtensions(filterExts);
		return dlg.open();
	}
}
